package com.bingo.generate.cli.Cmds;

import com.bingo.generate.cli.OldUse;
import com.bingo.generate.comm.service.GenerateDbService;
import com.bingo.generate.comm.util.JdbcUtil;
import org.apache.commons.lang3.StringUtils;
import picocli.CommandLine;

import java.util.List;
import java.util.Scanner;

@CommandLine.Command(
        subcommands = {},
        name = "gdb",
        description = "根据数据库生成实体代码(GenerateDbCmd)",
        sortOptions = false,
        mixinStandardHelpOptions = true
)
public class GenerateDbCmd implements Runnable {

    private static GenerateDbService service=GenerateDbService.ins;

    static {
        JdbcUtil.loadConfig();
    }

    @Override
    public void run() {
        System.out.println("欢迎来到我的时间-数据库的自动补全");
        Scanner sc= OldUse.sc;
        CommandLine commandLine = new CommandLine(new GdbRootCommand());
        while (true) {
            System.out.println(" -请输入你的命令(可以通过输入-h 查询命令/leaf代表离开当前界面):");
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String str = sc.nextLine();
            //  去除空格
            str = StringUtils.trim(str);
            //  如果是null，就重新弄一下
            if (StringUtils.isBlank(str)) {
                System.out.println("");
                System.out.println("请输入~~~");
                continue;
            }
            if("leaf".equals(str)){
                return;
            }
            String[] args=str.split(" ");
            commandLine.execute(args);
        }
    }

    // 将所有子命令装载在一起
    @CommandLine.Command(name = "generate-cli",
            subcommands = {
                    // command1
                    Config1.class,
                    Config2.class,
                    // command2
                    cmd2.class,
                    cmd3.class,
                    cmd4.class,
                    cmdGen1.class,
                    tipsCmdAdd.class
                    // 添加 yourCommand.class
            },
            mixinStandardHelpOptions = true
    )
    private class GdbRootCommand{

    }

    @CommandLine.Command(name = "rc",description = "读取数据库连接配置信息")
    private static class Config1 implements Runnable {

        /*@CommandLine.Option( names = {"-rc", "--readConnInfo"}, description = "读取数据库连接配置信息" )
        public String[] servers;

        @CommandLine.Option(names = {"-wc", "--writeConnInfo"},  description = "写入配置信息")
        public String user;*/

        @Override
        public void run() {
            //  寻找是否已经进行了配置(首先会去底层资源的的localTion找   然后c/user/appData/  )
            //  在命令行会让你进行配置
            //  首选判断你是否设置了location.properties,如果没有的话，提示你输入一下配置文件路径
            //  或者

            System.out.println(String.format("用户名:%s\n用户密码:%s\n连接URL:%s\n驱动信息:%s\n"
                    , JdbcUtil.username,JdbcUtil.pwd,JdbcUtil.url,JdbcUtil.driver
            ));
        }
    }

    @CommandLine.Command(name = "wc",description = "写入配置信息")
    private static class Config2 implements Runnable {
        @Override
        public void run() {
            System.out.println("这个输入是个测试版，没有任何其他用处");
            Scanner sc=OldUse.sc;
            System.out.print("请输入连接url:");
            JdbcUtil.url=sc.nextLine();
            System.out.println();
            System.out.print("请输入用户名:");
            JdbcUtil.username=sc.nextLine();
            System.out.println();
            System.out.print("请输入密码:");
            JdbcUtil.pwd=sc.nextLine();
            System.out.println();
            System.out.print("请输入连接驱动:");
            JdbcUtil.driver=sc.nextLine();
            System.out.println();
            System.out.println("输入完毕  可以使用rc命令查看配置信息 ");
            System.out.println("你的配置信息放在");
        }
    }

    @CommandLine.Command(
            name = "lt",
            description = "查询(数据库的表名)"
    )
    private static class cmd2 implements Runnable {

        @CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = " cmd cli help msg")
        public boolean help = false;

        @CommandLine.Option(names = {"-t", "--table"}, required = false, description = "查询表所需要的一些参数")
        public String tableNameKey;

        @CommandLine.Option(names = {"-d", "--dbname"}, required = false, description = "查询指定数据库的参数")
        public String dbNameKey;

        @CommandLine.Option(names = {"-c", "--column"}, required = false, description = "查询指定数据库的注释以及列名")
        public String columnKey;

        @Override
        public void run() {
            List<String> tables=service.searchTableName(dbNameKey,tableNameKey,columnKey);
            StringBuilder strBud=new StringBuilder();
            for (int i = 0; i < tables.size(); i++) {
                String table=tables.get(i);
                strBud.append(table).append("\t");
                if(i%7==0){
                    strBud.append("\n");
                }
            }
            System.out.println("查询出如下表名");
            System.out.println(strBud);
        }
    }

    @CommandLine.Command(
            name = "tipsAdd",
            description = "查询(数据库的表名)的相关列名  方便删除"
    )
    private static class tipsCmdAdd implements Runnable {

        @CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = " cmd cli help msg")
        public boolean help = false;

        @CommandLine.Option(names = {"-t", "--table"}, required = false, description = "查询表所需要的一些参数")
        public String tableNameKey;

        @CommandLine.Option(names = {"-d", "--dbname"}, required = false, description = "查询指定数据库的参数")
        public String dbNameKey;

        @CommandLine.Option(names = {"-c", "--column"}, required = false, description = "查询指定数据库的注释以及列名")
        public String columnKey;

        @CommandLine.Option(names = {"-pn", "--printName"}, required = false, description = "是否打印name")
        public boolean isPrintName=false;

        @Override
        public void run() {
            String str=service.tipsAdd(dbNameKey,tableNameKey,isPrintName);
            System.out.println(str);
        }
    }

    @CommandLine.Command(
            subcommands = {},
            name = "go",
            description = "根据表名输出实体类名称",
            sortOptions = false,
            mixinStandardHelpOptions = true
    )
    private static class cmd3 implements Runnable {

        @Override
        public void run() {
            System.out.println("还未实现该方法");
        }
    }

    @CommandLine.Command(
            subcommands = {},
            name = "g-check",
            description = "检测表或者实体是否字段或者属性发生了变化",
            sortOptions = false,
            mixinStandardHelpOptions = true
    )
    private static class cmd4 implements Runnable {

        @Override
        public void run() {
            System.out.println("还未实现该方法");
        }
    }

    @CommandLine.Command(
            name = "gmd",
            description = "根据数据库的表生成md的返回类型api说明文档"
    )
    private static class cmdGen1 implements Runnable {

        @CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = " cmd cli help msg")
        public boolean help = false;

        @CommandLine.Option(names = {"-t", "--table"}, required = false, description = "查询表所需要的一些参数")
        public String tableNameKey;

        @CommandLine.Option(names = {"-d", "--dbname"}, required = false, description = "查询指定数据库的参数")
        public String dbNameKey;

        /*@CommandLine.Option(names = {"-c", "--column"}, required = false, description = "查询指定数据库的注释以及列名")
        public String columnKey;*/

        @Override
        public void run() {
            String md=service.createMdByKey(dbNameKey,tableNameKey);
            StringBuilder strBud=new StringBuilder();
            /*for (String table : tables) {
                strBud.append(table).append("\t");
            }*/

            System.out.println("生成的md如下:");
            System.out.println(md);
        }
    }




}
